package com.gpframework.module.controller.purchase;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.module.purchase.domain.req.*;
import com.gpframework.module.purchase.service.IGpPurchaseIncomingInspectService;
import com.gpframework.module.purchase.service.IGpPurchaseTrackService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.purchase.domain.GpPurchaseReceipt;
import com.gpframework.module.purchase.domain.vo.GpPurchaseReceiptVo;
import com.gpframework.module.purchase.service.IGpPurchaseReceiptService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
/**
 * @ClassName GpPurchaseReceiptController
 * @Author gpframework
 * @Date 2023-12-22
 * @Version 1.0.0
 * @Description 采购收货单Controller
 */
@Validated
@Api(tags = "采购收货单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/purchase/purchaseReceipt" )
public class GpPurchaseReceiptController extends BaseController {

    private final IGpPurchaseReceiptService iGpPurchaseReceiptService;
    private final IGpPurchaseTrackService iGpPurchaseTrackService;
    private final IGpPurchaseIncomingInspectService iGpPurchaseIncomingInspectService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-22
  * @Param gpPurchaseReceipt
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('purchase:purchaseReceipt:list')")
    @GetMapping("/list")
    public TableDataInfo<GpPurchaseReceiptVo> list(GpPurchaseReceiptReq gpPurchaseReceiptReq)
    {
        startPage();
        QueryWrapper<GpPurchaseReceipt> lqw = new QueryWrapper<GpPurchaseReceipt>();
        gpPurchaseReceiptReq.generatorQuery(lqw,true);
        List<GpPurchaseReceiptVo> list = iGpPurchaseReceiptService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchaseReceipt
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('purchase:purchaseReceipt:add')" )
    @Log(title = "添加采购收货单" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpPurchaseReceiptAdd gpPurchaseReceiptAdd) {
        GpPurchaseReceipt gpPurchaseReceipt = Convert.convert(new TypeReference<GpPurchaseReceipt>() {}, gpPurchaseReceiptAdd);
        return toAjax(iGpPurchaseReceiptService.save(gpPurchaseReceipt) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchaseReceipt
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('purchase:purchaseReceipt:edit')" )
    @Log(title = "修改采购收货单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpPurchaseReceiptUpdate> updateList) {
        List<GpPurchaseReceipt> list = Convert.convert(new TypeReference<List<GpPurchaseReceipt>>() {}, updateList);
        return toAjax(iGpPurchaseReceiptService.updateBatchById(list) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchaseReceipt
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('purchase:purchaseReceipt:export')" )
    @Log(title = "导出采购收货单" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpPurchaseReceiptReq gpPurchaseReceiptReq) {
        QueryWrapper<GpPurchaseReceipt> lqw = new QueryWrapper<GpPurchaseReceipt>();
        gpPurchaseReceiptReq.generatorQuery(lqw,true);
        List<GpPurchaseReceiptVo> list = iGpPurchaseReceiptService.listVo(lqw);
        ExcelUtil<GpPurchaseReceiptVo> util = new ExcelUtil<GpPurchaseReceiptVo>(GpPurchaseReceiptVo. class);
        return util.exportExcel(list, "purchaseReceipt" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchaseReceipt
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('purchase:purchaseReceipt:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpPurchaseReceiptVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpPurchaseReceiptService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-22
     * @Param gpPurchaseReceipt
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('purchase:purchaseReceipt:remove')" )
    @Log(title = "删除采购收货单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpPurchaseReceiptService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 生成来料检验单
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpDeliveryNote
     * @Return TableDataInfo
     */
    @ApiOperation("生成来料检验单")
    @Log(title = "生成来料检验单" , businessType = BusinessType.INSERT)
    @PostMapping("/addIncomingInspect")
    public AjaxResult addIncomingInspect(@RequestBody GpPurchaseReceiptUpdate gpPurchaseReceiptUpdate) {
        if (gpPurchaseReceiptUpdate == null || CollectionUtils.isEmpty(gpPurchaseReceiptUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpPurchaseIncomingInspectService.addIncomingInspect(gpPurchaseReceiptUpdate) ? 1 : 0);
    }

    /**
     * @Description 生成采购跟踪单
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("生成采购跟踪单")
    @PreAuthorize("@ss.hasPermi('purchase:purchaseReceipt:add')" )
    @Log(title = "生成采购跟踪单" , businessType = BusinessType.INSERT)
    @PostMapping("/createPurchaseTrack")
    public AjaxResult createPurchaseTrack(@Valid @RequestBody GpProcureSchedulingUpdate gpProcureSchedulingUpdate) {
        if (gpProcureSchedulingUpdate == null || CollectionUtils.isEmpty(gpProcureSchedulingUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpPurchaseTrackService.createPurchaseTrack(gpProcureSchedulingUpdate.getIds(), gpProcureSchedulingUpdate.getCreateBy()) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('purchase:purchaseReceipt:add')" )
    @Log(title = "复制" , businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpPurchaseReceiptUpdate gpPurchaseReceiptUpdate) {
        if (gpPurchaseReceiptUpdate == null || gpPurchaseReceiptUpdate.getId() == null || gpPurchaseReceiptUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpPurchaseReceiptUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpPurchaseReceiptService.copy(gpPurchaseReceiptUpdate.getId(), gpPurchaseReceiptUpdate.getCopyNum()) ? 1 : 0);
    }
}
